/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Web;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace FourRoads.Links
{
	/// <summary>
	/// Summary description for ResLinkUrls.
	/// </summary>
	public class ResLinkUrls : ApplicationUrls
	{
		public static readonly string ResLinkUrlsProviderName = "ResLinkUrlsProvider";

		#region Instance

		private static ResLinkUrls _defaultInstance = null;

		static ResLinkUrls()
		{
			CreateDefaultCommonProvider();
		}

		/// <summary>
		/// Returns an instance of the user-specified data provider class.
		/// </summary>
		/// <returns>An instance of the user-specified data provider class.  This class must inherit the
		/// CommonDataProvider interface.</returns>
		public static ResLinkUrls Instance() 
		{
			return _defaultInstance;
		}

		public static ResLinkUrls Instance (Provider dataProvider) 
		{
			ResLinkUrls fdp = CSCache.Get(dataProvider.Name) as ResLinkUrls;
			if(fdp == null)
			{
				fdp = DataProviders.Invoke(dataProvider) as ResLinkUrls;
				CSCache.Max(dataProvider.Name,fdp);
			}
			return fdp;
		}

		/// <summary>
		/// Creates the Default CommonDataProvider
		/// </summary>
		private static void CreateDefaultCommonProvider()
		{
			// Read the configuration specific information
			// for this provider
			//
			Provider urlProvider = (Provider) CSConfiguration.GetConfig().Providers[ResLinkUrlsProviderName];

			// Create the instance for the provider, or fall back on the builtin one
			//
			if(urlProvider != null)
				_defaultInstance = CreateInstance(urlProvider) as ResLinkUrls;
			else
				_defaultInstance = new ResLinkUrls();
		}
        
		#endregion

		protected static string GetFriendlyPath(int sectionID)
		{
			return ResLinkSection.GetSection(sectionID).FreindlyPath;
		}

		private string GetUrlDependantOnModule(string urlSource ,  params object[] parameters)
		{
            if (Modules.LinksFriendlyName.UrlReWriteEnabled())
			{
				parameters[0] = GetFriendlyPath((int)parameters[0]);
				return FormatUrl(urlSource , parameters);
			}
			return FormatUrl(urlSource + "-NoFr" , parameters);
		}

		#region Public Urls

		public virtual string ViewLink(int sectionID , int postID)
		{	
			return GetUrlDependantOnModule("rs_viewSectionLink" , sectionID , postID);
		}

		public virtual string RssSection(int sectionID)
		{
			return  GetUrlDependantOnModule("rs_rssSection" , sectionID); 
		}

		public virtual string ViewSection(int sectionID)
		{
			return GetUrlDependantOnModule("rs_viewSection" , sectionID); 
		}

		public virtual string SuggestLink(int sectionID)
		{
			return FormatUrl("rs_SuggestLink" ,  sectionID);
		}

		public virtual string AddComment(int sectionID , int postID)
		{
			return FormatUrl("rs_LinkAddComment" ,  sectionID , postID);
		}

		public virtual string SectionAdmin(int sectionID)
		{
			return FormatUrl("rs_admin_Section_Admin" , sectionID);
		}

		public virtual string MoveLink(int sectionID , int postID , string returnURL)
		{
			return FormatUrl("rs_Moderate_Move_Link" , sectionID , postID, HttpContext.Current.Server.UrlEncode(returnURL));
		}

		public virtual string ModerationHome
		{
			get
			{
				return FormatUrl("rs_Moderation");
			}
		}

		public virtual string ModerationSection(int sectionID)
		{
			return FormatUrl("rs_Moderate_Section" , sectionID);
		}

		public virtual string DeadLinkSection(int sectionID)
		{
			return FormatUrl("rs_DeadLink_Section" , sectionID);
		}

		public virtual string LinkAdmin(int sectionID , int postID)
		{
			return FormatUrl("rs_admin_Link_Admin" , sectionID , postID);
		}

		public virtual string ModerateDeleteComment(int sectionID , int postID  , int commentID, string returnURL)
		{
			return FormatUrl("rs_Moderate_DeleteComment" , sectionID , postID , commentID , HttpContext.Current.Server.UrlEncode(returnURL));
		}

	
		
		#endregion
	}
}
